﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class PGRegisterControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        MPPersistence Persistence1;
        TextBox txtID, txt单位名称, txt入围年度;
        Label Label2, Label4;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] != null)
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            Persistence1 = (MPPersistence)FindControl("Persistence1");
            txtID = (TextBox)FindControl("txtID");
            txt单位名称 = (TextBox)FindControl("txt单位名称");
            txt入围年度 = (TextBox)FindControl("txt入围年度");
            Label2 = (Label)FindControl("Label2");
            Label4 = (Label)FindControl("Label4");
            Panel panel1 = (Panel)FindControl("panel1");
            string cid = base.Page.Request["CID"];
            if (!base.Page.IsPostBack)
            {
                Persistence1.Table = "zs_company";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["CID"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
            }
            Persistence1.BindControl = panel1;
            Button btn_Insert = (Button)FindControl("btn_Insert");
            btn_Insert.Click += new EventHandler(btnSave_Click);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string cid = base.Page.Request["CID"];
            if (string.IsNullOrEmpty(cid)) { 
                txtID.Text = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper();
                txt入围年度.Text = System.DateTime.Now.Year.ToString();
            } else { 
                txtID.Text = cid;
                string struserid = _EluZSDataProvider.GetSomeStr("zs_user_company", "userid", "cid", cid);
                if (!string.IsNullOrEmpty(struserid))
                {
                    EluZSProvider.User user = _EluZSDataProvider.GetUser(struserid);
                    Label2.Text = user.Loginid;
                    Label4.Text = user.Pwd;
                    
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int tt = Convert.ToInt32(_EluZSDataProvider.GetSomeWhere("zs_company", "count(*)", " 单位名称='" + txt单位名称.Text.Trim() + "' and 单位类别 ='评估机构'"));
            if (tt == 0)
            {
                string cid = txtID.Text;
                if (_EluZSDataProvider.ckuserinrole(cid))
                {
                    _EluZSDataProvider.checkuserrole("PG", "评估机构", cid, txt单位名称.Text, "");
                }
                Persistence1.Update();
                Antu.Utilities.UtilitiesPage.AlertDialog("注册成功！", string.Format("window.location.href('PGRegister.aspx?CID={0}');displaytable();", cid));
            }
            else {
                Antu.Utilities.UtilitiesPage.AlertDialog("此单位名称已存在！", "");
            }
        }


    }
}


